//
//  TimeFunctions.m
//  LoveLive
//
//  Created by 穆相臣 on 6/16/16.
//  Copyright © 2016 穆相臣. All rights reserved.
//

#import "TimeFunctions.h"

@implementation TimeFunctions

+(NSString *)timeWithData:(NSDictionary *)data
{
    NSString * string = [data valueForKey:@"iso"];
    string = [string substringToIndex:19];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
    
    NSString *strDate;
    
    NSDate * localDate = [TimeFunctions dateAddYear:0 Month:0 Day:0 hour:8 withData:[dateFormatter dateFromString:string]];

    double between = [NSDate date].timeIntervalSince1970 - localDate.timeIntervalSince1970;
    if (between < 7 * 24 * 60 * 60)
    {
        // day
        if (between < 24 * 60 * 60)
        {
            // hour
            if (between < 60 * 60)
            {
                // min
                if (between < 60)
                {
                    // second
                    strDate = [NSString stringWithFormat:@"%d秒前",(int)between];
                }
                else
                {
                    strDate = [NSString stringWithFormat:@"%d分钟前",(int)between/60];
                }
            }
            else
            {
                strDate = [NSString stringWithFormat:@"%d小时前",(int)between/60/60];
            }
        }
        else
        {
            strDate = [NSString stringWithFormat:@"%d天前",(int)between/24/60/60];
        }
    }
    else
    {
        NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
//        [dateFormatter1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        [dateFormatter1 setDateFormat:@"yyyy-MM-dd"];

        strDate = [dateFormatter1 stringFromDate:localDate];
    }
  
    return strDate;
}

+(NSString *)normalTimeWithData:(NSDictionary *)data
{
    NSString * string = [data valueForKey:@"iso"];
    string = [string substringToIndex:19];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
    
    NSString *strDate;
    
    NSDate * localDate = [TimeFunctions dateAddYear:0 Month:0 Day:0 hour:8 withData:[dateFormatter dateFromString:string]];
    
        NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
        //        [dateFormatter1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        [dateFormatter1 setDateFormat:@"yyyy-MM-dd"];
        
        strDate = [dateFormatter1 stringFromDate:localDate];
    
    return strDate;
}

+(NSDate *)dateWithData:(NSDictionary *)data
{
    NSString * string = [data valueForKey:@"iso"];
    string = [string substringToIndex:19];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
    
    NSDate * localDate = [TimeFunctions dateAddYear:0 Month:0 Day:0 hour:8 withData:[dateFormatter dateFromString:string]];

    return localDate;
}

+(NSString *)shortTimeWithData:(NSNumber *)data
{
    NSDate * date = [NSDate dateWithTimeIntervalSince1970:(data.doubleValue + 8 * 60 * 60)];
    return [[date description]substringWithRange:NSMakeRange(12, 4)];
}

+(NSString *)leanCloudDate:(NSDate *)date
{
    NSDate * newData = [TimeFunctions dateAddYear:0 Month:0 Day:0 hour:-8 withData:date];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
    
    NSString * stringDate = [dateFormatter stringFromDate:newData];
    stringDate = [stringDate stringByAppendingString:@".543Z"];
    NSLog(@"%@",stringDate);
    
    return stringDate;
}

+(NSString *)lastUpdateTimeToday
{
    NSString * stirng = [[[NSDate date]description]substringToIndex:11];
    stirng = [stirng stringByReplacingOccurrencesOfString:@"-" withString:@""];
    NSLog(@"%@",stirng);
    return stirng;
}

+(NSDate *)dateAddYear:(int)year Month:(int)month Day:(int)day hour:(int)hour withData:(NSDate *)date {
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    //NSCalendarIdentifierGregorian:iOS8之前用NSGregorianCalendar
    NSDateComponents *comps = nil;
    
    comps = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:date];
    //NSCalendarUnitYear:iOS8之前用NSYearCalendarUnit,NSCalendarUnitMonth,NSCalendarUnitDay同理
    
    NSDateComponents *adcomps = [[NSDateComponents alloc] init];
    
    [adcomps setYear:year];
    
    [adcomps setMonth:month];
    
    [adcomps setDay:day];
    
    [adcomps setHour:hour];
    
    return [calendar dateByAddingComponents:adcomps toDate:date options:0];
}

+(NSString *)ymdDate:(NSDate *)date
{
    NSDate * newData = [TimeFunctions dateAddYear:0 Month:0 Day:0 hour:-8 withData:date];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSString * stringDate = [dateFormatter stringFromDate:newData];
    NSLog(@"%@",stringDate);
    
    return stringDate;
}
@end
